package com.tucc.hello.leetcode.no1_400.no1_200.no1_20.no1_10.no1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {2, 7, 11, 15};
        int target = 13;
        int[] result = solution.twoSum(nums, target);
        if (result != null) {
            System.out.println(result[0] + " " + result[1]);
        }
    }

    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> numMap = new HashMap<>();
        numMap.put(nums[0], 0);
        for (int i = 1; i < nums.length; i++) {
            Integer targetIndex = numMap.get(target - nums[i]);
            if (targetIndex != null) {
                return new int[]{targetIndex, i};
            }
            numMap.put(nums[i], i);
        }
        return null;
    }

}
